<?php
/**
 * <class description>
 *
 * @author Matias Mirabelli <project@moyrax.com>
 *
 * @package Kingorm
 */
class KingormCoreBean
{
	private $includePath = array (
		"mappings"  => "mappings/",
		"builders"  => "mappings/builders/"
	);

	private $kingormCore = array (
		"CModelEvents", "CatalogBase", "DAOTemplate", "Link", "Token",
		"PreparedStatement"
	);

	public static $configure = null;

	public static $classLoader = null;

	/**
	 * Carga el contexto de Kingorm.
	 *
	 * @param $foghornPath Ruta de acceso relativa
	 */
	public function __construct( $configClass, ClassLoader $contextLoader )
	{
		// Obtiene el path relativo de Kingorm.
		//
		$foghornPath = $this->getSelfPath();

		// Carga las clases del sistema.
		//
		$contextLoader->loadClasses( $this->kingormCore );

		// Carga la configuración.
		//
		$contextLoader->loadClass( $configClass );

		// Guarda la configuración global.
		//
		self::$configure = $contextLoader->uniqueInstance( $configClass );

		self::$configure->kingormRoot = $foghornPath;

		// Agrega los directorios de Kingorm al contexto actual.
		//
		$contextLoader->setEnvironVar( "KINGORM_ROOT", $foghornPath );
		$contextLoader->addContextPaths( self::$configure->kingormIncludes );

		foreach( $this->includePath as $key => $value )
		{
			$contextLoader->addIncludePath( $value );
		}
	}

	private function getSelfPath()
	{
		$reflector = new ReflectionClass( $this );

		$filename = $reflector->getFileName();

		$filename = str_replace( get_class($this) . ".php", "", $filename );

		return( $filename );
	}
}
?>
